﻿using System;
using System.Collections.Generic;
using System.Linq;
using LS.Entitys;
using LS.Entitys.system;
using System.Data;
using System.Text;
using LS.Common;
using System.Threading.Tasks;

namespace LS.Cores.Services.system
{
    public class SysNoticeService : BaseService<SysNotifyEntity>, ISysNotice
    {



        /// <summary>
        /// 获得未读消息的数量
        /// </summary>
        /// <returns></returns>
        public DataTable GetUnNotifyNum(string where)
        {
            string sql = $@"select (select count(1) from sys_notify_content a inner join sys_notify b on a.id=b.contentid and b.notifystatus=0 {where} where a.notifyType=0 ) as Notify0,
            (select count(1) from sys_notify_content a  inner join sys_notify b on a.id = b.contentid and b.notifystatus = 0  {where}  where a.notifyType = 1) as Notify1,
            (select count(1) from sys_notify_content a  inner join sys_notify b on a.id = b.contentid and b.notifystatus = 0  {where}  where a.notifyType = 2) as Notify2";
            return SqlHelper.QueryDataTable(sql);
        }
        /// <summary>
        /// 设置未读消息为已读
        /// </summary>
        /// <param name="id">要设置的消息id</param>
        /// <returns></returns>
        public int SetNotifyToRead(string id)
        {
            string sql = "update sys_notify set notifystatus=1,readDate=@readDate where id=@id";
            return SqlHelper.Execute(sql, new { id = id, readDate = DateTime.Now });
        }
        /// <summary>
        /// 设置未读消息为已读
        /// </summary>
        /// <param name="notifyType"></param>
        /// <returns></returns>
        public int SetAllNotifyToRead(string notifyType)
        {
            string sql = "update sys_notify set notifystatus=1,readDate=@readDate where contentid in(select id from sys_notify_content where notifyType=@notifyType)";
            return SqlHelper.Execute(sql, new { notifyType = notifyType, readDate = DateTime.Now });
        }
        //插入消息
        public int InsertNotify(SysNotifyContentEntity notifyContent, List<SysNotifyEntity> notifyList)
        {
            bool resulte = SqlHelper.ExecuteTranSql((con, tran) =>
            {
                int r = 0;
                try
                {
                    StringBuilder strsql = new StringBuilder();
                    strsql.Append("insert into sys_notify_content(senderId,content,notifyType,createDate,isdel) ");
                    strsql.Append(" values(@senderId,@content,@notifyType,@createDate,0)");
                    strsql.Append(" ;SELECT CAST(SCOPE_IDENTITY() as int)");
                    //r = SqlHelper.ExecuteCon(strsql.ToString(), notifyContent, tran, null, CommandType.Text, con);
                    int contentid = SqlHelper.QueryIdentityCon(strsql.ToString(), notifyContent, tran, false, null, CommandType.Text, con);
                    if (notifyList != null && notifyList.Count > 0)
                    {
                        notifyList.ForEach(a => a.ContentId = contentid);
                        StringBuilder strsqlfun = new StringBuilder();
                        strsqlfun.Append("insert into sys_notify(dialogueId,senderId,recipientId,contentId,createDate,notifystatus,isdel,source,sourceId) ");
                        strsqlfun.Append(" values(@dialogueId,@senderId,@recipientId,@contentId,getdate(),0,0,@source,@sourceId)");
                        r = SqlHelper.ExecuteCon(strsqlfun.ToString(), notifyList, tran, null, CommandType.Text, con);
                    }
                }
                catch (Exception ex)
                {
                    Log4NetHelper.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType).Debug(ex.Message);
                    r = 0;
                }

                return r;
            });
            return resulte ? 1 : 0;
        }

        /// <summary>
        /// 删除消息
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        public int DeleteNotify(string ids)
        {
            StringBuilder sql = new StringBuilder();
            sql.Append(" update sys_notify set  isdel =1 where id in (" + ids + ")  ");
            return SqlHelper.Execute(sql.ToString());
        }
    }
}
